package com.blogs.component;

import java.util.ArrayList;

import com.blogs.entity.Author;
import com.blogs.tools.DBSharedPreferences;
import com.blogs.tools.ImageDownLoad;
import com.cnblogs.R;
import com.cnblogs.R.color;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AuthorListAdapter extends BaseAdapter{
	private  ArrayList<Author> authorList = null;
	private ViewHolder holder;
	private Activity context = null;
	private DBSharedPreferences sp;
	public AuthorListAdapter(Activity context, ArrayList<Author> authorList)
	{
		this.context = context;
		this.authorList = authorList;
		sp= new DBSharedPreferences(context);
	}
	public int getCount() {
		// TODO Auto-generated method stub
		return authorList.size();
	}
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return authorList.get(position);
	}
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null ) {
			convertView = View.inflate(context, R.layout.author_adapter_template,null);
			holder=new ViewHolder();
			holder.author_adapter_head_iv=(ImageView)convertView.findViewById(R.id.author_adapter_head_iv);
			holder.author_adapter_username=(TextView)convertView.findViewById(R.id.author_adapter_username);
			holder.author_adapter_create_time_tv=(TextView)convertView.findViewById(R.id.author_adapter_create_time_tv);
			holder.author_adapter_article_tv=(TextView)convertView.findViewById(R.id.author_adapter_article_tv);
			holder.home_adapter_mask=(ImageView)convertView.findViewById(R.id.home_adapter_mask);
			convertView.setTag(holder);
		}
	    else
	    {
	    	holder = (ViewHolder) convertView.getTag();
	    }
//		holder.author_adapter_head_iv.set(authorList.get(position).avatar);
		if(!authorList.get(position).avatar.equals(""))
		{
			ImageDownLoad.loadDrawable(authorList.get(position).avatar, holder.author_adapter_head_iv);
		}
		else
		{
			holder.author_adapter_head_iv.setImageResource(R.drawable.sample_face);
		}
		holder.author_adapter_username.setText(authorList.get(position).name);
		holder.author_adapter_create_time_tv.setText(authorList.get(position).updated);
		holder.author_adapter_article_tv.setText("("+authorList.get(position).postcount+")博文");
		if(sp.GetIsDarkState())
		{
			holder.home_adapter_mask.getBackground().setLevel(2);
			holder.author_adapter_create_time_tv.setTextColor(context.getResources().getColor(color.con_text_night));
			holder.author_adapter_article_tv.setTextColor(context.getResources().getColor(color.con_text_night));
			holder.author_adapter_username.setTextColor(context.getResources().getColor(color.con_butom_night));
		}
		else
		{
			holder.home_adapter_mask.getBackground().setLevel(1);
			holder.author_adapter_create_time_tv.setTextColor(context.getResources().getColor(color.con_text_light));
			holder.author_adapter_article_tv.setTextColor(context.getResources().getColor(color.con_text_light));
			holder.author_adapter_username.setTextColor(context.getResources().getColor(color.con_text_light));
		}
		return convertView;
	}

	class ViewHolder 
	{
		ImageView author_adapter_head_iv;
		TextView author_adapter_username;
		TextView author_adapter_create_time_tv;
		TextView author_adapter_article_tv;
		ImageView home_adapter_mask;
	}
}
